package com.lqfan.autoAnswerQ.service.fileType.impl;

import com.lqfan.autoAnswerQ.model.fileType.FileType;
import com.lqfan.autoAnswerQ.dao.fileType.FileTypeMapper;
import com.lqfan.autoAnswerQ.model.fileType.HandleFileType;
import com.lqfan.autoAnswerQ.model.quesBank.Question;
import com.lqfan.autoAnswerQ.service.fileType.IFileTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lqfan
 * @since 2020-04-04
 */
@Service
public class FileTypeServiceImpl extends ServiceImpl<FileTypeMapper, FileType> implements IFileTypeService {
    private static final String filePath = System.getProperty("user.dir");
    @Override
    @Transactional
    public boolean saveFileType(String path, int type, String userId) {
        // 文件路径对象  文件路径保存、文件所在分类、用户id、文件上级目录
        FileType fileType = new FileType();
        fileType.setFilePath(path);
        fileType.setType(type);
        fileType.setUserId(userId);
        String[] s = path.split("/");
        String pPath = path.substring(0,path.lastIndexOf("/"));
        String getPid = baseMapper.selectFilePid(pPath.substring(0,pPath.lastIndexOf("/")+1));
        String pid= "0";
        if (pPath!=null&&s.length>1){
            pid = getPid;
        }else {
            pid = "0";
        }

        fileType.setPid(pid);
        boolean saveResult = save(fileType);
        return saveResult;
    }

    public String selectFilePid(String filePath){
        String getPid = baseMapper.selectFilePid(filePath);
        return getPid;
    }

    @Override
    public boolean deleteFileType(String path, String userId) {
        boolean deleteFileType = baseMapper.deleteFileType(path,userId);
        return deleteFileType;
    }

    @Override
    public boolean deleteFiles(String path, String userId) {
        boolean deleteFileType = baseMapper.deleteFiles(path,userId);
        return deleteFileType;
    }

    @Override
    public  List<HandleFileType> getPath(String userId,int type,String questionName) {
        List<HandleFileType> paths = baseMapper.getPath(userId,type,questionName);
        return paths;
    }

    @Override
    public  List<HandleFileType> getFilePath(String userId, int type, String questionName) {
        List<HandleFileType> paths = baseMapper.getFilePath(userId,type,questionName);
        return paths;
    }
}
